<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->plugin_setting('setting');
if($_RQ['wxgid']){
	$id=pdo_getcolumn('core_account_wxapp',array('id'=>$_RQ['wxgid'],'uniacid'=>$_SESSION['uniacid']),'plugin_key');
}
$_RQ['id']=$_RQ['id']?:$_RQ['merchid'];
if($_RQ['id'] && !$id){
	$id=$_RQ['id'];
}
$id=$id?:$_SESSION['merchid'];
$mid=$_RQ['fromuser']?:$_RQ['mid'];
$mid=$mid?:$_SESSION['uid'];
$return['params']=array();
if($_RQ['op']=='main'){
	if(!$id){
		exi('该商户不存在！','error');
	}
	$this->session('merchid',$id);
	$goodsid=intval($_RQ['goodsid']);
	$return['fromgoods']=array();
	if($goodsid){
		$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
		$return['fromgoods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),$fields);
		$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
		$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$return['fromgoods']['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
		$return['fromgoods']['cartid']=$cartinfo['id']?:0;
		$return['fromgoods']['carttotal']=$cartinfo['total']?:0;
		$return['fromgoods']['cartprice']=$cartinfo['price']?:0;
	}
	$myshop=pdo_get('jy_merch_user',array('id'=>$id));
	$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
	$myshop['out_price']=$myshop['out_price']>0?$myshop['out_price']:$citypeisong['out_price'];
	//$myshop['out_pay']=$myshop['out_pay']>0?$myshop['out_pay']:$citypeisong['out_price'];
	$myshop['goodscount']=pdo_count('jy_weishop_goods',array('merchid'=>$id));
	$myshop['ordercount']=pdo_count('jy_weishop_order',array('merchid'=>$id));
	$myshop['salenum']=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$id),'total');
	$myshop['lat_lng']=json_decode($myshop['lat_lng'],true);
	$myshop['onlinetime']=$myshop['onlinetime']?json_decode($myshop['onlinetime'],true):array('star'=>'07:00','end'=>'22:00');
	$myshop['onlinetime']['star']=$myshop['onlinetime']['start']?:'7:00';
	$myshop['onlinetime']['end']=$myshop['onlinetime']['end']?:'22:00';	
	$days=date('Y-m-d',TIMESTAMP);
	$star=strtotime($days.' '.$myshop['onlinetime']['star']);
	$end=strtotime($days.' '.$myshop['onlinetime']['end']);
	if($end<$star){
		$end=$end+24*3600;
	}
	if($star>TIMESTAMP || TIMESTAMP>$end){
		$myshop['offline']=1;
	}
	$goodsids=pdo_getall('jy_weishop_goods',array('merchid'=>$id),'id','id');
	if($goodsids){
		$goodsids=array_keys($goodsids);
		$myshop['tags']=pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids),'tags');
	}
	$tags=str_array($return['pluginset']['tags'],'|');
	$key=mt_rand(0,count($tags));
	$myshop['tags']=$myshop['tags']?:$tags[$key];
	$myshop['coupons']=model('jy_weishop/coupon')->getmerchcoupon($myshop['id'],1);
	$myshop['logo']=tomedia($myshop['logo']);
	$myshop['pcd']=json_decode($myshop['pcd'],true);
	$myshop['address']=$myshop['pcd']['city'].' '.$myshop['pcd']['district'].' '.$myshop['pcd']['street'].' '.$myshop['address'];
	$return['myshop']=$myshop;
	$return['kefu']=$myshop['iskefu'];
	$return['setting']=$setting;
	$return['params']=array('page'=>1,'merchid'=>$id);
	//补充
	$category=pdo_getall('jy_weishop_goods_group',array('merchid'=>$myshop['id'],'uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>0,'onepluginname OR'=>'onepluginname=\'jy_merch\' or onepluginname=\'\''),'','','paixu desc');
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'merchid'=>0,'isshow'=>1,'parentid'=>0),'','','paixu desc');
	}
	$getcarts=model('jy_weishop/cart')->getall(1,$id,true);
	$carts=$getcarts['list'];
	$allnum=$getcarts['alltotal'];
	$goodsprice=$getcarts['goodsprice'];
	foreach($category as $k=>&$v){
		$allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate2'=>$v['id']),'id');
		if(empty($allgoods)){
		  $allgoods=pdo_getall('jy_weishop_goods',array('merchid'=>$myshop['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate'=>$v['id']),'id');  
		}
		$v['total']=0;
		foreach($allgoods as $ag){
			$carttotal=pdo_getsum('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$v['total'] +=$carttotal;
		}
		if(empty($allgoods)){
			unset($category[$k]);
		}
	}
	$othergoods=pdo_getall('jy_weishop_goods',array('merchid'=>$id,'status'=>1,'uniacid'=>$_SESSION['uniacid'],'pcate2'=>0),'id');
	$total=0;
	foreach($othergoods as $ag){
			$carttotal=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$ag['id'],'selected'=>1,'mid'=> $_SESSION['uid'],'uniacid' => $_SESSION['uniacid']),'total');
			$total +=$carttotal;
	}
	$category=array_values($category);
	if($_RQ['getgoodslist'] || $_SESSION['getgoodslist']){
		$_SESSION['getgoodslist']=1;
		$category=model('jy_merch/goods')->upcategorygoods($category,$myshop['id'],$_RQ);
	}
	//运费计算：
	$order=array(
		'list'=>$carts,
		'total_price'=>$goodsprice,
	);
	$up_dispatch=model('jy_weishop/order/dispatch')->up_dispatch($order,array(),'',$_SESSION['store_id'],$myshop);
	$return['dispatchprice']=$up_dispatch['get_freight']['fee'];
	$allprice = $goodsprice - $youhui;
	$return['category']=$category;
	$return['nowckey']=0;
	$return['nowctitle']=$category[0]['title'];
	$return['params']['pcate']='all';
	$return['allnum']=$allnum;
	$return['totalprice']=$allprice;
	$return['carts']=$carts;
	$return['showlist']='goods';
	if($_RQ['orderid']){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'id'=>$_RQ['orderid']),array('id','status','tableid'));
	}
	//凭号下单
	$return['tableid']=0;
	$tableid = intval($_RQ['tableid'])?:$order['tableid'];
	if($tableid){
		$this->session('tableid',$tableid);
	}else{
		$tableid=$this->session('tableid');
	}
	if($tableid){
		$table = pdo_get('jy_weishop_table',array('merchid'=>$myshop['id'],'uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
		$this->session('tableid',$tableid);
		$return['tableid']=$tableid;
		if(!$order['id']){
			$order=pdo_get('jy_weishop_order',array('tableid'=>$tableid,'uniacid'=>$_SESSION['uniacid'],'status'=>0,'createtime >'=>TIMESTAMP-3*3600),array('id','status','tableid'));
			if($order['id']){
				//如果清台了，订单不算
				if(!$table['busy']){
					$order=array();
				}
			}
		}
	}
	$return['oldorderid']=$order['id'];
	$this->session('oldorderid',$return['oldorderid']);
	$return['order']=$order;
	$usernum = intval($_RQ['usernum']);
	if($usernum){
		$this->session('usernum',$usernum);
		$this->session('childnum',intval($_RQ['usernum']));
	}
	$usernum=$this->session('usernum');
	if(!$usernum && $tableid && !$return['oldorderid']){
		exi('','','jy_merch/getqueue//from=table&merchid='.$myshop['id']);
	}
	if($tableid){
		//不是我在用
		if($table['nowuserid'] !=$_SESSION['uid']){
			if($table['status'] <1){
				$this->session('tableid','');
				exi('不好意思，该餐桌未开放，请选择其他餐桌！','error','jy_weishopmeituan/merchitem//id='.$myshop['id']);
			}
			if($table['hebingid'] && $table['busy']==4){
				exi('餐桌已被合并，请让主餐桌扫码点餐!','error','jy_weishopmeituan/merchitem//id='.$myshop['id']);
			}
			if($table['busy']>0 && empty($myshop['in_add'])){
				//$this->session('tableid','');
				//exi('餐桌已被占用，请重新选择餐桌!','error','jy_weishopmeituan/merchitem//id='.$myshop['id']);
			}
		}
		if(!$table['busy']){
			pdo_update('jy_weishop_table',array('usernum'=>$usernum,'childnum'=>$this->session('childnum'),'up_time'=>time(),'is_get'=>0,'scanningnum +'=>1,'busy'=>1,'nowuserid'=>$_SESSION['uid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
		}
	}
	$return['table']=$table;
	if($this->plugin_exists('jy_merch')){
		$jy_merchsetting=plugin('jy_merch')->plugin_setting('setting');
	}
	if($jy_merchsetting['is_bigwheel'] && $this->plugin_exists('jy_bigwheel')){
		$return['bigwheelgames']=model('jy_bigwheel/model')->merchgame($myshop['id']);
	}
	$sharelink='jy_weishopmeituan/merchitem//id='.$id.'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$myshop['merchname'],
			'desc'=>$myshop['desc']?$myshop['desc']:'美团外卖，一个属于自己的美团外卖！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($myshop['logo'])
	);
	set_html('title',$myshop['merchname']);
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'nochecked'=>0,'merchid'=>$this->session('merchid'));
	if($_RQ['pcate']){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if($_RQ['ccate']){
		$params['ccate']=intval($_RQ['ccate']);
		$return['params']['ccate']=intval($_RQ['ccate']);
	}
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
		unset($params['pcate']);
		unset($params['ccate']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 6;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','pcate','active_plugin','active_id','salenum','pluginname','originalprice','total','weight','unit');
	$orderby='salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		foreach($list as &$itemv){
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				//已选规格
				$selectspecs = array();
				$selespename = '';
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
							$selectspecs[$k]=$sp['items'][0]['id'];
							$selespename .=$sp['items'][0]['title'].'，';
						}
				}
				//默认已选参数：
				$spsids = '';
				foreach($selectspecs as $k=>&$s){
						if($k==0){
							$spsids .= $s;
						}else{
							$spsids .= '_'.$s;
						}
				}
				if($store_id){
					$getinfo=model('jy_weishop/store')->getinfo($itemv['id'],$store_id);
					$itemv['total']=$getinfo['id']?$getinfo['total']:$itemv['total'];
					$itemv['price']=$getinfo['id']?$getinfo['price']:$itemv['price'];
					$itemv['options']=model('jy_weishop/store')->getoption($itemv['id'],$store_id,$spsids);
				}
				if(!$itemv['options']['id']){
					$itemv['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$itemv['id'],'specs'=>$spsids));
				}
				//默认商品规格价格：
				if($itemv['options']){
					$itemv['price']=$itemv['options']['price'];
					if($itemv['active']['discount']>10){
						$itemv['options']['oldprice']=$itemv['options']['price'];
						$itemv['price']=$itemv['options']['oldprice']*$itemv['active']['discount']/100;
						$itemv['options']['price']=$itemv['price'];
					}
					$itemv['post'][$itemv['options']['id']]=array('num'=>0,'title'=>$selespename);
					$op_carts=pdo_getall('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),array('id','optionid','total'));
					foreach($op_carts as $ov){
						$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$ov['optionid']),'specs');
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$optitle=implode("，",$specs);
						$itemv['post'][$ov['optionid']]=array('num'=>$ov['total']?:0,'title'=>$optitle);
					}
				}
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$itemv['optionid']=$itemv['options']['id'];
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
		}
	}
	exi($list);
}
if($_RQ['op']=='check_buy'){
	$merchid=intval($_RQ['merchid']);
	$sendtype=intval($_RQ['sendtype']);
	$tableid=intval($_RQ['tableid']);
	$oldorderid=$this->session('oldorderid');
	$prams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1);
	if($merchid){
		if($merchid==-1){
			$prams['merchid']=0;
		}else{
			$prams['merchid']=$merchid;
		}
	}
	$cart_list=pdo_getall('jy_weishop_member_cart',$prams,array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list && !$oldorderid){
		exi('您的购物车没有选中商品！','error');
	}
	model('jy_weishop/cart')->check_buy($cart_list,'','',1);
	model('jy_weishop/activity/sales')->check_sale_new($cart_list);
	$query='sendtype='.$sendtype.'&merchid='.$merchid.'&tableid='.$tableid.'&id='.$oldorderid;
	if($oldorderid){
		$query .='&goodslist=1';
	}
	$resurl='jy_weishopmeituan/create//'.$query;
	if($_RQ['isresurl']){
		exi($resurl);
	}else{
		exi('','',$resurl);
	}
}
?>